package com.shujvku.test3.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.shujvku.test3.bean.User;
import com.shujvku.test3.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/*
 **@Author:刘淑钰
 **Created on 2021/11/28
 */
@RestController
public class LoginController {

    //dao层完成后通过spring的注入，放到controller中，自动注入
    @Autowired
    UserDao userDao;
    //获取前台提交的用户和密码，封装在User对象里
    //接下来要在Controller中调用dao层，完成数据库的操作，根据数据库操作的结果，返回给前台
    @RequestMapping("/login")
    public String login(@RequestBody User user){

        String flag="error";
        User us=userDao.getUserByMassage(user.getUsername(),user.getPassword());
        HashMap<String,Object> res=new HashMap<>();
        if(us!=null){flag="ok";}
        res.put("flag",flag);
        res.put("user",user);
        String res_json = JSON.toJSONString(res);
//        System.out.println("User : " + us);
        return res_json;
    }
}
